package com.codegero.android.client.sms.gateway.boundary;

import java.util.List;

import com.codegero.android.client.sms.gateway.R;
import com.codegero.android.client.sms.gateway.entity.RegistryVote;
import com.codegero.android.client.sms.gateway.entity.Vote;
import com.codegero.android.client.sms.gateway.stored.DatabaseManager;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class DaftarRegistryVote extends ListActivity implements OnClickListener {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.general_list);
		TextView title = (TextView) findViewById(R.id.title);
		title.setText("Daftar Registry Vote");
		databaseManager = new DatabaseManager(this);
		registryVotes = databaseManager.getAllRegistryVotes(false);
		mAdapter = new AdapterDaftarRegistryVote
				(this, R.layout.general_item, registryVotes);
		ListView listView = getListView();
		listView.setAdapter(mAdapter);

		ImageView button = (ImageView) findViewById(R.id.buttonGeneral);
		button.setOnClickListener(this);
	}
	
	private void refresh(){
		registryVotes = databaseManager.getAllRegistryVotes(false);
		mAdapter.notifyDataSetChanged();
	}
	
	private class AdapterDaftarRegistryVote extends ArrayAdapter<RegistryVote>{

		public AdapterDaftarRegistryVote(Context context,
				int textViewResourceId, List<RegistryVote> objects) {
			super(context, textViewResourceId, objects);
			this.context 	= context;
			this.resource 	= textViewResourceId;
			registryVotes = objects;
		}
		
		@Override
		public int getCount() {
			return registryVotes!=null?registryVotes.size():0;
		}
		

		@Override
		public RegistryVote getItem(int position) {
			return registryVotes.get(position);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			final RegistryVote registryVote = getItem(position);
			
			if(registryVote == null) return null;
			
			LayoutInflater inflater = (LayoutInflater) context
			        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			View rowView = inflater.inflate(resource, parent, false);
			
			TextView id 		= (TextView) rowView.findViewById(R.id.generalId);
			TextView name	= (TextView) rowView.findViewById(R.id.generalMessage);
			
			id.setText(registryVote.getId());
			name.setText(registryVote.getNama());
			
			if(position%2==0){
				rowView.setBackgroundColor(0xCCFFCC);
			}else{
				rowView.setBackgroundColor(0xFFFFFF);
			}
			rowView.setOnLongClickListener(new View.OnLongClickListener() {
				
				public boolean onLongClick(View arg0) {
					Bundle bundle = new Bundle();
					bundle.putString("id", registryVote.getId());
					bundle.putString("name", registryVote.getNama());
					showDialog(RESPONSE_CHOOSE, bundle);
					return true;
				}
			});
			return rowView;
		}
		
		private List<RegistryVote> registryVotes;
		private final Context context;
		private final int resource;
	}
	
	
	@Override
	protected Dialog onCreateDialog(int id, final Bundle bundle) {
		//TODO :  create dialog open it!!! 
		switch(id){
			case RESPONSE_ADD:{
				final Dialog dialog = new Dialog(this);
				dialog.setContentView(R.layout.registry_vote_add);
				final TextView tvKode = (TextView) dialog.findViewById(R.id.registryTxtKode);
				final TextView tvName = (TextView) dialog.findViewById(R.id.registryTxtName);
				if(bundle!=null){
					tvKode.setText(bundle.getString("id"));
					tvName.setText(bundle.getString("name"));
				}
				Button buttonOk		= (Button) dialog.findViewById(R.id.registryBtnOk);
				buttonOk.setOnClickListener(new View.OnClickListener() {
					
					public void onClick(View v) {
						RegistryVote registryVote = new RegistryVote();
						registryVote.setId(tvKode.getText().toString());
						registryVote.setNama(tvName.getText().toString());
						if(bundle!=null){
							databaseManager.updateRegistryVote(registryVote);
						}else{
							databaseManager.addRegistryVote(registryVote);
						}
						dialog.dismiss();
						refresh();
					}
				});
				Button buttonCancel	= (Button) dialog.findViewById(R.id.registryBtnCancel);
				buttonCancel.setOnClickListener(new View.OnClickListener() {
					
					public void onClick(View v) {
						dialog.dismiss();
					}
				});
				return dialog;
			}case RESPONSE_CHOOSE:{
				final Dialog dialog = new Dialog(this);
				dialog.setContentView(R.layout.choose_general);
				
				Button btnDetail = (Button) dialog.findViewById(R.id.chooseBtnDetail);
				btnDetail.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						Intent intent = new Intent(DaftarRegistryVote.this,DaftarVote.class);
						intent.putExtras(bundle);
						dialog.dismiss();
						refresh();
					}
				});
				
				Button btnEdit = (Button) dialog.findViewById(R.id.chooseBtnEdit);
				btnEdit.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						dialog.dismiss();
						showDialog(RESPONSE_ADD, bundle);
					}
				});
				
				Button btnDelete = (Button) dialog.findViewById(R.id.chooseBtnDelete);
				btnDelete.setOnClickListener(new View.OnClickListener() {
					
					public void onClick(View v) {
						RegistryVote registryVote = new RegistryVote();
						registryVote.setId(bundle.getString("id"));
						registryVote.setNama(bundle.getString("name"));
						databaseManager.deleteRegistryVote(registryVote);
						Vote vote = new Vote();
						vote.setId(registryVote.getId());
						databaseManager.deleteVote(vote);
						dialog.dismiss();
						refresh();
					}
				});
				return dialog;
			}
		}
		return super.onCreateDialog(id);
	}
	
	public void onClick(View v) {
		switch(v.getId()){
			case R.id.buttonGeneral:{
				showDialog(RESPONSE_ADD);
				break;
			}
		}
	}


	private ArrayAdapter<RegistryVote> mAdapter;
	private List<RegistryVote> registryVotes;
	private DatabaseManager databaseManager;
	
	private static final int RESPONSE_ADD 	= 0;
	private static final int RESPONSE_CHOOSE= 1;

}
